package com.db.williamchart.renderer.executor;

import com.db.williamchart.data.AxisType;
import com.db.williamchart.data.Paddings;

public class MeasureLineChartPaddings {

    public Paddings invoke(
            AxisType axisType,
            float labelsHeight,
            float longestLabelWidth,
            float labelsPaddingToInnerChart,
            float lineThickness,
            int pointsDrawableWidth,
            int pointsDrawableHeight
    ) {
        Paddings labelsPaddings = measureLabelsPaddingsX(
                axisType, labelsHeight, labelsPaddingToInnerChart)
                .mergeWith(
                        measureLabelsPaddingsY(
                                axisType, labelsHeight, longestLabelWidth, labelsPaddingToInnerChart)
                );

        float left = labelsPaddings.getLeft() + lineThickness / 2 + (float) pointsDrawableWidth / 2;
        float top = labelsPaddings.getTop() + lineThickness + (float) pointsDrawableHeight / 2;
        float right = labelsPaddings.getRight() + lineThickness / 2 + (float) pointsDrawableWidth / 2;
        float bottom = labelsPaddings.getBottom() + lineThickness + (float) pointsDrawableHeight / 2;

        return new Paddings(left, top, right, bottom);
    }

    private Paddings measureLabelsPaddingsX(
            AxisType axisType,
            float labelsHeight,
            float labelsPaddingToInnerChart
    ) {
        return new Paddings(
                0,
                0,
                0,
                axisType.shouldDisplayAxisX() ? labelsHeight + labelsPaddingToInnerChart : 0
        );
    }

    private Paddings measureLabelsPaddingsY(
            AxisType axisType,
            float labelsHeight,
            float longestLabelWidth,
            float labelsPaddingToInnerChart
    ) {
        if (!axisType.shouldDisplayAxisY()) {
            return new Paddings(0, 0, 0, 0);
        }

        return new Paddings(
                longestLabelWidth + labelsPaddingToInnerChart,
                labelsHeight / 2,
                0,
                labelsHeight / 2
        );
    }
}
